package com.dure.hotmemory.adapters;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.App;
import com.dure.hotmemory.listeners.ConfirmListener;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leix on 14/12/2016.
 */

public class AppAdapter extends BaseAdapter {
    List<App> appList = new ArrayList<>();
    Context context;
    ConfirmListener confirmListener;

    public AppAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return appList.size();
    }

    @Override
    public App getItem(int i) {
        return appList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.recommend_item_layout, null);
            viewHolder.appIV = (ImageView) view.findViewById(R.id.app_iv);
            viewHolder.downloadIV = (ImageView) view.findViewById(R.id.download_iv);
            viewHolder.nameTV = (TextView) view.findViewById(R.id.name_tv);
            viewHolder.descTV = (TextView) view.findViewById(R.id.desc_tv);
            viewHolder.sizeTV = (TextView) view.findViewById(R.id.size_tv);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final App app = getItem(i);
        x.image().bind(viewHolder.appIV, app.icon);
        viewHolder.nameTV.setText(app.app_name);
        viewHolder.descTV.setText(app.desc);

        viewHolder.downloadIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (confirmListener != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("app_name", app.app_name);
                    bundle.putString("url", app.url);
                    confirmListener.confirmed(bundle);
                }
            }
        });
        return view;
    }

    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public void setData(List<App> appList) {
        if (appList != null && appList.size() > 0) {
            this.appList.clear();
            addData(appList);
        }
    }

    public void addData(List<App> appList) {
        String currentPackageName = context.getPackageName();
        int index = -1;
        for (int i = 0; i < appList.size(); i++) {
            App app = appList.get(i);
            if (TextUtils.equals(app.package_name, currentPackageName)) {
                index = i;
                break;
            }
        }
        if (index != -1)
            appList.remove(index);
        this.appList.addAll(appList);
        notifyDataSetChanged();
    }

    class ViewHolder {
        ImageView appIV;
        TextView nameTV;
        TextView descTV;
        TextView sizeTV;
        ImageView downloadIV;
    }
}
